package sg.edu.nus.iss.cerberus.engine;

import java.util.HashMap;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import sg.edu.nus.iss.cerberus.model.CClass;
import sg.edu.nus.iss.cerberus.util.CerberusConstants;

/**
 * A class is for creation of CClass.
 * 
 * @author
 * */

public class CClassBuilder {
	
	private IJavaElement element;
	private IJavaProject javaProject;
	private IType type;
	private HashMap<String,CClass> classobj;
	
	public CClassBuilder(IJavaElement element) {
		this.element=element;	
		javaProject = element.getJavaProject();
		classobj = new HashMap<String, CClass>();
		type= null;
	}
	
	public HashMap<String,CClass> getCClassCollection() {
		if (element != null) {
			IProject proj=javaProject.getProject();
			String clsName = element.getElementName();
			IFile file = ResourcesPlugin.getWorkspace().getRoot().getProject(proj.getName()).getFile(clsName);
			try {
				//gives package name
				String pkg=element.getParent().getElementName().toString();
				for(int i=0;i<2;i++)
				{	
					String cls=clsName;
					/* For getting the source file CClass object*/	
					if(i == 0) {
						cls=clsName.substring(CerberusConstants.TEST_ANNOTATION_NAME.length(), clsName.length()-CerberusConstants.TEST_CLASS_EXTENSION.length());
					}
					/* For getting the test file CClass object*/
					else {
						cls=clsName.substring(0, clsName.length()-CerberusConstants.TEST_CLASS_EXTENSION.length());
						}
					type = javaProject.findType(pkg+CerberusConstants.DOT+cls);
					classobj.put(cls, new TestEngineParser(javaProject, type).parseCompilationUnit());
				}
			} catch (JavaModelException e1) {
				e1.printStackTrace();	}		
		}
		return classobj;
	}

	public IType getType() {
		return type;
	}


	}
